﻿using System;
using System.Collections.Generic;

namespace OrderManagement
{
    public class OrderDetails
    {
        public int OrderDetailsId { get; set; }
        public Product Product { get; set; }
        public int Quantity { get; set; }
        public double Price { get; set; }

        public OrderDetails(int orderDetailsId, Product product, int quantity, double price)
        {
            OrderDetailsId = orderDetailsId;
            Product = product;
            Quantity = quantity;
            Price = price;
        }

        public override bool Equals(object obj)
        {
            return obj is OrderDetails details &&
                   OrderDetailsId == details.OrderDetailsId;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(OrderDetailsId);
        }

        public override string ToString()
        {
            return "[商品名称：" + Product.ProductName +
                   "，单价：" + Price +
                   "，数量：" + Quantity +
                   "，明细总金额：" + Price * Quantity + "]";
        }
    }


    public class Order
    {
        public int OrderId { get; set; }
        public Customer Customer { get; set; }
        public List<OrderDetails> OrderDetails { get; set; }
        public double TotalAmount
        {
            get
            {
                double totalAmount = 0;
                foreach (OrderDetails detail in OrderDetails)
                {
                    totalAmount += detail.Price * detail.Quantity;
                }
                return totalAmount;
            }
        }

        public Order(int orderId, Customer customer, List<OrderDetails> orderDetails)
        {
            OrderId = orderId;
            Customer = customer;
            OrderDetails = orderDetails;
        }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   OrderId == order.OrderId;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(OrderId);
        }

        public override string ToString()
        {
            string orderDetailsString = "";
            foreach (OrderDetails detail in OrderDetails)
            {
                orderDetailsString += detail.ToString() + "\n";
            }
            return "订单编号：" + OrderId +
                   "\n客户信息：" + Customer.ToString() +
                   "\n订单总金额：" + TotalAmount +
                   "\n订单明细：" + orderDetailsString;
        }
    }
}

